﻿using System;
using System.Data;
using Guru.Data;
using Telerik.Web.UI;
using Guru.Web.Controls.Asp;

public partial class FE_Invoices : HSMPage
{
    protected const string LINK_INVOICE_DETAIL = "<a class='rgLinkIcon rgLinkEdit' onclick=\"OpenPopupModal(1, 'CheckOutModal.aspx?{1}');\">{0}</a>";

    protected void uxInvoicesGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            DataRowView dv = item.DataItem as DataRowView;
            CheckBox received = item.FindControl("uxReceivedMoney") as CheckBox;
            LinkButton print = item.FindControl("uxPrint") as LinkButton;

            string secParams = BuildSecureQueryString(string.Format("InvoiceID={0}&FromInvoices=1", dv["InvoiceID"].ToString()));
            print.OnClientClick = string.Format("OpenPopupModal(1, 'InvoiceModal.aspx?{0}'); return false;", secParams);

            item["InvoiceNumber"].Text = string.Format(LINK_INVOICE_DETAIL, dv["InvoiceNumber"], secParams);
            if (dv["ReceivedMoney"].IsNotNull())
            {
                received.Checked = bool.Parse(dv["ReceivedMoney"].ToString());              
            }
            else
            {
                received.Checked = false;
            }            
        }
    }
    protected void uxInvoicesGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        GuruParameterCollection parameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        parameters.Add("@InvoiceNumber", this.uxInvoiceNumber.Text.Trim(), DbType.AnsiString);
        if (uxFromDate.SelectedDate != null)
        {
            parameters.Add("@FromDate", uxFromDate.SelectedDate.Value.Date, DbType.Date);
        }
        if (uxToDate.SelectedDate != null)
        {
            parameters.Add("@ToDate", uxToDate.SelectedDate.Value.Date, DbType.Date);
        }
        DataTable dt = HSMCommon.HSMBusiness.GetDataTable("spa_HSM_Invoice_GetAll", parameters);
        uxInvoicesGrid.DataSource = dt;
    }

    protected void uxSearch_Click(object sender, EventArgs e)
    {
        uxInvoicesGrid.Rebind();
    }
}
